package com.accounting.client.frontend.order;

import com.accounting.client.remote.Services;
import com.accounting.client.remote.enumeration.Courier;
import com.accounting.client.remote.enumeration.OrderStatues;
import com.accounting.client.remote.obj.Order;
import com.accounting.client.remote.transfer.SearchOrderRequest;
import com.accounting.client.session.ErrorHandler;
import com.accounting.client.session.SessionManager;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;

import corelib.client.Util;
import corelib.client.delegate.DataCallback;
import corelib.client.search.JSONSearchRequest;
import corelib.client.search.PagingResult;
import corelib.client.ui.SimpleMask;
import corelib.client.ui.widget.WidgetListener;
import corelib.client.ui.widget.button.Button;
import corelib.client.ui.widget.form.DateBox;
import corelib.client.ui.widget.form.LazyInitializer;
import corelib.client.ui.widget.form.SelectField;
import corelib.client.ui.widget.form.TextField;
import corelib.client.ui.widget.form.fomatters.ValueFormatter;
import corelib.client.ui.widget.layout.RelativeColumn;
import corelib.client.ui.widget.panel.SimpleSearchPanel;

public class OrderSearchPanel extends SimpleSearchPanel {
	
	private OrderManager       transactionPage;
	private SelectField<Integer> statusField;
	private SelectField<Courier> courierField;
	private TextField       customerNameField;
	private DateBox 		   dateFromField;
	private DateBox            dateToField;
	
	private DataCallback<PagingResult<Order>> callback;
	
	public OrderSearchPanel(OrderManager page, DataCallback<PagingResult<Order>> callback){
		this.setWidth(450);
		this.transactionPage = page;
		this.callback 		 = callback;
	}
	
	@Override
	protected Widget createContentPanel() {
		courierField = new SelectField<Courier>(Courier.All, new ValueFormatter<Courier>(){
			@Override
			public String format(Courier value) {
				if (value == null){
					return "请选择快递";
				}
				return value.getCName();
			}
		});
		courierField.setNullable(true);
		
		statusField = LazyInitializer.createSelectBox(OrderStatues.All, new ValueFormatter<Integer>(){
			@Override
			public String format(Integer value) {
				if (value == null){
					return "请选择订单状态";
				}
				return OrderStatues.getName(value);
			}
	    });
	    statusField.setNullable(true);
	    
		int lw  = 100;
		int fw  = 250;
		int gap = 12;
		
		FlowPanel panel = new FlowPanel();
		panel.getElement().getStyle().setPadding(20, Unit.PX);
		
		RelativeColumn col = new RelativeColumn();
		col.setRowSpacing(12);
		col.addRow()
		   .add(LazyInitializer.createLabel("快递").setBold(true),lw)
		   .addSpace(gap)
		   .add(courierField,fw);
		
		col.addRow()
		   .add(LazyInitializer.createLabel("交易状态").setBold(true),lw)
		   .addSpace(gap)
		   .add(statusField,fw);
		
		col.addRow()
		   .add(LazyInitializer.createLabel("收件人").setBold(true), lw)
		   .addSpace(gap)
		   .add(customerNameField = LazyInitializer.createTextBox("按客户姓名搜索"),fw);
		
		col.addRow()
		   .add(LazyInitializer.createLabel("创建日期").setBold(true), lw+fw);
		
		col.addRow()
		   .addSpace(4)
		   .add(LazyInitializer.createLabel("起始日期"),lw)
		   .addSpace(gap)
		   .add(dateFromField = new DateBox(),fw);
		
		col.addRow()
		   .addSpace(4)
		   .add(LazyInitializer.createLabel("结束日期"),lw)
		   .addSpace(gap)
		   .add(dateToField = new DateBox(),fw);
		
		Button searchButton = new Button("搜索", new WidgetListener(){
			@Override
			public void valueUpdated(Widget widget) {
				search();
			}
		});
		
		Button resetButton = new Button("重置", new WidgetListener(){
			@Override
			public void valueUpdated(Widget widget) {
				reset();
				search();
			}
		});
		
		col.setRowSpacing(20);
		col.addRow().addSpace(lw + gap).add(searchButton,100).addSpace(gap).add(resetButton,100);
		panel.add(col.getPanel());
		return panel;
	}
	
	@Override
	protected Widget getMaskWidget() {
		return this.transactionPage.getOrderTable();
	}
	
	public void reset(){
		dateToField.reset();
		dateFromField.reset();
		courierField.setSelectedOption(null);
		statusField.setSelectedOption(null);
		customerNameField.setText(Util.EMPTY_STRING);
	}
	
	public void search() {
		SearchOrderRequest request = getSearchRequest();
		SimpleMask.show(getMaskWidget(),"Searching...");
		
		Services.OrderService.SearchOrders(SessionManager.getPassport(), request, 
			new AsyncCallback<PagingResult<Order>>(){
				@Override
				public void onFailure(Throwable caught) {
					SimpleMask.hide(getMaskWidget());
					ErrorHandler.handleServiceError(caught);
				}
				@Override
				public void onSuccess(PagingResult<Order> result) {
					SimpleMask.hide(getMaskWidget());
					if (callback != null){
					    callback.onCompleted(result);
					}
				}
			}
		);
	}
	
	public SearchOrderRequest getSearchRequest() {
		SearchOrderRequest request = new SearchOrderRequest();
		request.setOffset(0);
		request.setLimit(OrderManager.MaxRecordsPerPage);
		request.setCustomerName(customerNameField.getTrimmedText());
		request.setDateFrom(dateFromField.getDate());
		request.setDateTo(dateToField.getDate());
		request.setStatus(statusField.getSelectedOption());
		request.setCourierId(courierField.getSelectedOption() == null ? null: courierField.getSelectedOption().getCode());
		return request;
	}
	
	@Override
	protected JSONSearchRequest createSearchRequest() {
		return new JSONSearchRequest();
	}

}
